package unite

import (
	"fmt"
	"github.com/hashicorp/consul/api"
)

type Client struct {
	client *api.Client
}

type ConsulRegister struct {
	ID      string
	Name    string
	Tags    []string
	Port    int
	Address string
}

func NewConsul(host string, port int) *Client {
	config := api.DefaultConfig()
	config.Address = fmt.Sprintf("%v:%v", host, port)

	client, err := api.NewClient(config)
	if err != nil {
		return nil
	}
	return &Client{client: client}
}

func (c *Client) Regis(register ConsulRegister) error {
	a := new(api.AgentServiceRegistration)
	a.ID = register.ID
	a.Name = register.Name
	a.Tags = register.Tags
	a.Address = register.Address
	a.Port = register.Port

	err := c.client.Agent().ServiceRegister(a)
	if err != nil {
		return err
	}
	return err

}
